<template>
    <div>
        <ButtonGroup shape="circle">
            <Button type="ghost" @click="print">打印处方笺</Button>
            <Button type="ghost" @click="search">刷新</Button>
        </ButtonGroup>
        <Table style="margin-top: 5px" border :columns="columns" :data="data" @on-selection-change="changeSelect"></Table>
    </div>
</template>

<script>
    import iviewTable from '@/view/common/table/Table.vue';
    import iviewPage from '@/view/common/page/Page.vue';
    export default {
        components: {
            iviewTable,
            iviewPage
        },
        data () {
            return {
                data: [],
                selectRows: [],
                baseURL: '/user/treat/plan/product',
                columns: [
                    {
                        type: 'selection',
                        width: 60,
                        align: 'center'
                    },{
                        title: '创建时间',
                        key: 'creationTime',
                        render: (h, params) => {
                            return h('span', this.$moment(params.row.creationTime).format('YYYY年MM月DD日'))
                        }
                    },{
                        title: '处方产品',
                        key: 'name'
                    },{
                        title: '包装规格',
                        key: 'packSpecific'
                    },{
                        title: '计量单位',
                        key: 'unitName'
                    },{
                        title: '用法',
                        key: 'instructions',
                        render: (h, params) => {
                            var vm = this
                            return h('Input',{
                                props: {
                                    type: 'textarea',
                                    rows: '2',
                                    placeholder: '请输入用法',
                                    value: params.row.instructions
                                },
                                style: {
                                    width: '100%',
                                    margin: '5px 0px 5px 0px'
                                },
                                on: {
                                    'on-blur' (event) {
                                        params.row.instructions = event.target.value
                                        vm.data[params.index] = params.row
                                    }
                                }
                            });
                        }
                    }
                ]
            };
        },
        methods: {
            search () {
                if (this.$store.state.app.complaintId) {
                    let url = this.baseURL + '/findToxic/' + this.$store.state.app.complaintId
                    this.$ajax.get(url).then(r => {
                        let {code, data, msg} = r.data
                        if (code === 0) {
                            this.data = data
                        } else {
                            this.$bus.alert('error', msg)
                        }
                    })
                }
            },
            init () {
                this.search()
            },
            print () {
                if (this.selectRows.length === 0) {
                    this.selectRows = this.data
                }
                let datas = []
                for (let row of this.selectRows) {
                    datas.push(row.id + '-' + row.instructions + '-' + row.quantity + '$' + row.totalPrice)
                }
                this.selectRows = []
                let url = this.$store.state.app.WEBROOT + '/user/print/printToxic?username=' + this.$store.state.app.loginUser.username + '&patientId=' + this.$store.state.app.patientId + '&datas=' + datas
                window.open(url)
            },
            changeSelect (rows) {
                this.selectRows = rows
            },
            changeTotalSize (totalSize) {
                this.pageModel.totalSize = totalSize
            },
            changePageNumber (pageNumber) {
                this.pageModel.pageNumber = pageNumber
            },
            changePageSize (pageSize) {
                this.pageModel.pageSize = pageSize
            }
        },
        mounted () {
            // this.init()
        }
    }
</script>

<style>

</style>
